package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.dao.SysDepartLimitMapper;
import com.yungu.swift.system.sys.dao.SysWayMapper;
import com.yungu.swift.system.sys.model.dto.SysDepartLimitDto;
import com.yungu.swift.system.sys.model.enums.SysErrorEnum;
import com.yungu.swift.system.sys.model.param.AdminDepartLimitPageParam;
import com.yungu.swift.system.sys.model.vo.SimpleWayInfo;
import com.yungu.swift.system.sys.model.vo.SysDepartLimitVo;
import com.yungu.swift.system.sys.service.SysDepartLimitService;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 出发时间限制
 */
@Service
public class SysDepartLimitServiceImpl extends BaseServiceImpl<SysDepartLimitDto> implements SysDepartLimitService {

    @Autowired
    private SysDepartLimitMapper sysDepartLimitMapper;

    @Autowired
    private SysWayMapper sysWayMapper;

    @Override
    protected IMapper<SysDepartLimitDto> getMapper() {
        return sysDepartLimitMapper;
    }

    @Override
    public ResponseData<PageVo<SysDepartLimitVo>> queryPageVo(AdminDepartLimitPageParam departLimitPageParam) {
        PageBounds pageBounds = new PageBounds(departLimitPageParam.getPageNum(), departLimitPageParam.getPageSize());
        PageList<SysDepartLimitVo> pageList = sysDepartLimitMapper.queryListPage(MapUtils.convertObjToMap(departLimitPageParam), pageBounds);
        PageVo<SysDepartLimitVo> pageVo = new PageVo(departLimitPageParam.getPageNum(), departLimitPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<SysDepartLimitVo> addOrEdit(AdminDepartLimitPageParam departLimitPageParam) {
        Map<String, Object> paraMap = new HashMap<>();
        SysDepartLimitVo sysDepartLimitVo = new SysDepartLimitVo();
        //如果UUID不为空 则为编辑情况 查出该限制信息
        if (StringUtils.isNotEmpty(departLimitPageParam.getUuid())) {
            paraMap.put("uuid", departLimitPageParam.getUuid());
            List<SysDepartLimitDto> list = sysDepartLimitMapper.list(paraMap);
            if (CollectionUtils.isEmpty(list)) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未找到线路限制信息");
            }
            BeanUtils.copyProperties(list.get(0), sysDepartLimitVo);
            //设置已勾选的线路
            String[] limitWayInfos = sysDepartLimitVo.getLimitWayUuid().split(",");
            List<SimpleWayInfo> checkLimitWayList = new ArrayList<>();
            for (String key : limitWayInfos) {
                SimpleWayInfo simpleWayInfo = new SimpleWayInfo();
                simpleWayInfo.setWayUuid(key);
                checkLimitWayList.add(simpleWayInfo);
            }
            sysDepartLimitVo.setLimitWayInfo(checkLimitWayList);
        }
        //获取全部线路信息
        paraMap.clear();
        List<SimpleWayInfo> wayList = sysWayMapper.getWayInfo(paraMap);
        if (CollectionUtils.isEmpty(wayList)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未找到系统线路信息");
        }
        //设置所有线路信息 按照城市分组
        Map<String, List<SimpleWayInfo>> groupMap = new HashMap<>();
        for (SimpleWayInfo simpleWayInfo : wayList) {
            if (groupMap.containsKey(simpleWayInfo.getCityName())) {
                groupMap.get(simpleWayInfo.getCityName()).add(simpleWayInfo);
            } else {
                List<SimpleWayInfo> wayByCity = new ArrayList<>();
                wayByCity.add(simpleWayInfo);
                groupMap.put(simpleWayInfo.getCityName(), wayByCity);
            }
        }
        //遍历分组MAP 取出分组结果
        List<Map<String, Object>> allWayList = new ArrayList<>();
        for (String key : groupMap.keySet()) {
            Map<String, Object> allWayMap = new HashMap<>();
            allWayMap.put("cityName", key);
            allWayMap.put("wayInfo", groupMap.get(key));
            allWayList.add(allWayMap);
        }
        sysDepartLimitVo.setAllWayInfo(allWayList);


        return ResponseData.buildSuccessResponse("校验成功", sysDepartLimitVo);
    }


    @Override
    public ResponseData<Boolean> checkDepartLimit(AdminDepartLimitPageParam departLimitPageParam) {
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("wayUuid",departLimitPageParam.getLimitWayUuid());
        paraMap.put("deparTime",departLimitPageParam.getDeparTime());
        SysDepartLimitVo sysDepartLimitVo = sysDepartLimitMapper.checkDepartLimit(paraMap);
        if (sysDepartLimitVo != null) {
            ObjectNode jsonNode = JsonUtils.newJsonNode();
            jsonNode.put("title", "该线路出发时间不可用");
            jsonNode.put("cueWord", sysDepartLimitVo.getLimitCueWord());
            return ResponseData.buildErrorResponse(SysErrorEnum.DEPARTIME_LIMIT.getCode(), jsonNode.toString());
        }
        return ResponseData.buildSuccessResponse(Boolean.TRUE);
    }
}